/**
 * Book.js 为每一个竖排的簿记组件
 */
import React, { PureComponent } from 'react';
import _ from 'lodash';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { Row, Col, Input, message, Select } from 'antd';
import { PrimaryGhostC, PrimaryC, SelectC } from '../../../../comps';

import { connect } from 'dva';
import { config1 } from '../config';

import shareOptionStyle from '../shareOptionStyle.less';

import TopButtonC from './TopButtonC';
import BookVanilla from './BookVanilla';
import BaseOption from './BaseOption';
import BookBarrier from './BookBarrier';
import BookDual from './BookDual';
const FormItem = Form.Item;
const { Option } = Select;
@Form.create()
@connect(({ shareOptionManageM, bookM, globalM }) => ({ shareOptionManageM, bookM, globalM }))
export default class Book extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      dataSource: this.props.dataSource,
      isShow: false, // 选择了期权结构则为true，按钮点亮 表单出来
      structure: '', //期权结构
    };
  }
  componentDidMount() {
    const { dataSource } = this.props;
    // 复用和编辑的时候回填期权结构
    this.setState({
      structure: dataSource.structure,
      isShow: dataSource.structure ? true : false,
    });
  }
  // 保存
  save = () => {
    const { dataSource, dispatch } = this.props;
    const { structure } = this.state;
    const bookMain = this.$BaseOption.getSettingInfo();
    if (!bookMain) {
      message.error('基础期权信息填写不规范');
      return false;
    }
    const bookVanilla = this.$BookVanilla.getSettingInfo();
    if (!bookVanilla) {
      message.error('香草期权信息填写不规范');
      return false;
    }
    const bookBarrier = this.$BookBarrier.getSettingInfo();
    if (!bookBarrier) {
      message.error('障碍期权信息填写不规范');
      return false;
    }
    const bookDual = this.$BookDual.getSettingInfo();
    if (!bookDual) {
      message.error('二元期权信息填写不规范');
      return false;
    }
    let vo = {
      structure,
      info: [
        {
          bookMain,
          bookVanilla: structure === '1' ? bookVanilla : null,
          bookBarrier: structure === '2' ? bookBarrier : null,
          bookDual: structure === '3' ? bookDual : null,
        },
      ],
    };

    if (dataSource.bundleId && dataSource.bundleId !== '') {
      // 更新

      dispatch({
        type: 'bookM/upDataOneBook',
        payload: {
          bundleId: dataSource.bundleId,
          ...vo,
        },
        callback: (status, data) => {
          if (status === 'success') {
            message.success('更新成功');
            dispatch({
              type: 'bookM/save',
              payload: {
                pageIsChange: true,
              },
            });
          }
        },
      });
    } else {
      //新增
      dispatch({
        type: 'bookM/saveOneBook',
        payload: vo,
        callback: (status, data) => {
          if (status === 'success') {
            message.success('保存成功');
            dispatch({
              type: 'bookM/save',
              payload: {
                pageIsChange: true,
              },
            });
          }
        },
      });
    }
  };
  // 删除
  delete = () => {
    const {
      bookM: { mapData },
      dispatch,
      dataSource,
    } = this.props;
    // 本地删除
    let data = _.cloneDeep(mapData);
    const newData = data.filter((ele) => {
      return ele.key !== dataSource.key;
    });
    dispatch({
      type: 'bookM/save',
      payload: { mapData: newData },
    });
    // 线上删除
    if (dataSource.bundleId && dataSource.bundleId !== '') {
      dispatch({
        type: 'shareOptionManageM/deleteOptionByBookId',
        payload: { bundleId: dataSource.bundleId },
        callback: (status) => {
          if (status === 'success') {
            message.success('删除成功');
            dispatch({
              type: 'bookM/save',
              payload: {
                pageIsChange: true,
              },
            });
          }
        },
      });
    }
  };
  // 试算
  account = () => {
    message.success('簿记执行试算');
  };
  // 期权结构变化
  structChange = (e) => {
    const {
      form: { setFieldsValue },
    } = this.props;
    this.setState(
      {
        isShow: true,
        structure: e,
      },
      () => {
        // 带出期权结构
        this.$BaseOption.structureChange(e);
      },
    );
  };
  render() {
    const { isShow, structure } = this.state;
    const {
      dataSource,
      form: { getFieldDecorator },
      bookM: { titleOpen },
      globalM: { dataDictionary: dictionary },
    } = this.props;

    const ds1 = dictionary['OTC_OPTION_BOOK_OPTIONSTRUCT']
      ? dictionary['OTC_OPTION_BOOK_OPTIONSTRUCT'].map((ele) => {
          return { title: ele.value, value: ele.code };
        })
      : [];
    return (
      <div className='formDiv'>
        {/* 期权结构 */}
        <div style={{ height: '39px', marginBottom: '10px' }}>
          <SelectC
            dataIndex='structure'
            form={this.props.form}
            dataSource={ds1}
            initialValue={dataSource.structure}
            onChange={this.structChange}
            style={{ width: '100%', height: '39px' }}
          />
        </div>

        <div style={{ height: isShow ? 'auto' : '0px', overflow: 'hidden' }}>
          {/* 基础期权信息 */}
          <div>
            <BaseOption
              bookMain={dataSource.info[0].bookMain}
              structure={structure}
              onRef={(ref) => {
                this.$BaseOption = ref;
              }}
            />
          </div>
          {/* 香草 */}
          {/* 留下与标题等高的空格 */}
          <div style={{ height: '49px' }}></div>
          <div style={{ height: titleOpen.isOpen2 ? 'auto' : '0px', overflow: 'hidden' }}>
            <BookVanilla
              bookVanilla={dataSource.info[0].bookVanilla}
              structure={structure}
              onRef={(ref) => {
                this.$BookVanilla = ref;
              }}
            />
          </div>
          {/* 障碍 */}
          {/* 留下与标题等高的空格 */}
          <div style={{ height: '49px' }}></div>
          <div style={{ height: titleOpen.isOpen3 ? 'auto' : '0px', overflow: 'hidden' }}>
            <BookBarrier
              bookBarrier={dataSource.info[0].bookBarrier}
              structure={structure}
              onRef={(ref) => {
                this.$BookBarrier = ref;
              }}
            />
          </div>
          {/* 二元 */}
          {/* 留下与标题等高的空格 */}
          <div style={{ height: '49px' }}></div>
          <div style={{ height: titleOpen.isOpen4 ? 'auto' : '0px', overflow: 'hidden' }}>
            <BookDual
              bookDual={dataSource.info[0].bookDual}
              structure={structure}
              onRef={(ref) => {
                this.$BookDual = ref;
              }}
            />
          </div>
        </div>
      </div>
    );
  }
}
